/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package agenda.dao;

import agenda.bean.Tabela;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Antonio
 */
public class PesquisaDAO {
    private Connection conn;

    public PesquisaDAO() {
        conn = Conexao.getInstance();
    }

    public Tabela executaConsulta(String sql, String parametro) {
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, parametro);
            ResultSet rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int qtde = rsmd.getColumnCount();
            Tabela tabela = new Tabela();
            for (int i = 1; i <= qtde; i++) {
                tabela.adicionaColuna(rsmd.getColumnLabel(i));
            }
            while (rs.next()) {
                Vector<Object> novaLinha = new Vector<Object>();
                for (int i = 1; i <= qtde; i++) {
                    novaLinha.add(rs.getObject(i));
                }
                tabela.adicionaLinha(novaLinha);
            }
            rs.close();
            return tabela;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

}


















